package framework.classification.entities;

import java.util.ArrayList;
import java.util.HashMap;

import framework.classification.entities.lattice.TreillisI;
import framework.classification.library.AbstractClassfication;
import framework.entity.ACLComponent;
import framework.entity.ACLEntity;
import framework.entity.ACLFunction;
import framework.entity.ACLInterface;

public class ClComponentProvided extends ClAbstractEntity{

	private ArrayList<ACLComponent> components;

	public static String COMPONENTPRO = "Component_Provided";

	public ClComponentProvided(String s, AbstractClassfication cl, TreillisI t,
			ArrayList<ACLComponent> a) {
		super(s, cl, t);
		components = a;
		createTableModel();
		int x = this.getAttribut().size();
		int y = this.getEntity().size();
		table = new int[x][y];
		calculPair();

	}


	public void calculPair() {

		for (int x = 0; x < getAttribut().size(); x++) {
			for (int y = 0; y < getEntity().size(); y++) {
				if (isPair(getAttribut().get(x), getEntity().get(y), y))
					table[x][y] = 1;
			}

		}

	}

	public Boolean isPair(String attribut, String component, int y) {

		//on parle le role de l'interface attribut required ou provided
		String strole = attribut.substring(0,attribut.indexOf("="));
		String strInterAtt = attribut.substring(attribut.indexOf('=')+1);
		ACLInterface aclInterAtt = cl.getInterfacesNames().get(strInterAtt);

		//on recupere la classification de cette interface
		ClInterfaceProvided clproAtt = getAbstractClassfication().getInterfacesProvided().get(aclInterAtt.getName());

		//pour toutes les interfaces contenues dans le composant
		ArrayList<ACLInterface> interEntity = getAbstractClassfication().getStructure().getComponentsProvided().get(component);
		for (ACLInterface aclInterEntity :interEntity){
			//on regarde si l'interface du comopsant est une interface enfant de l'interface attribut
			if (clproAtt.getLattice().isParent(aclInterAtt.toString(),aclInterEntity.toString()))
				return true;
		}
		return false;
	}




	public void createTableModel() {

		//liste comportant toutes les Interfaces de top niveau des differentes classifications;
		ArrayList<String> topInters = new ArrayList<String>();


		//on ajoute les composants dans les colonnes
		for (ACLComponent aclcomp : components){
			getEntity().add(aclcomp.getName());

			//----------------------- Interface Provided
			//on ajoutes les interfaces fournis des differents composants
			for (ACLInterface aclInterpro : aclcomp.getInterfacesPro()){
				//on recupere la classification de l'interface
				//et on y ajoute toutes les interfaces comrises
				ClInterfaceProvided clpro =getAbstractClassfication().getInterfacesProvided().get(aclInterpro.getName());
				for (String interpro : clpro.getRealReduced().keySet()){
					ACLInterface aclinter = new ACLInterface(interpro,ACLFunction.MODE2);

					if (!getAttribut().contains("Pro="+aclinter.getName()))
						getAttribut().add("Pro="+aclinter.getName());
				}
				for (String interpro : clpro.getAbstractReduced().keySet()){
					ACLInterface aclinter = new ACLInterface(interpro,ACLFunction.MODE2);

					if (!getAttribut().contains("Pro="+aclinter.getName()))
						getAttribut().add("Pro="+aclinter.getName());
				}
			}



		}




















	}

	public String getType() {
		// TODO Auto-generated method stub
		return COMPONENTPRO;
	}


	@Override
	public ACLEntity getACLEntity(String s) {
		// TODO Auto-generated method stub
		return null;
	}


	



}
